class CaijispsController < ApplicationController
  # GET /caijisps
  # GET /caijisps.xml
  def daoru
require 'hpricot'
require 'open-uri'
  sshopdate=Canshu.find(1)
	@doc=""
   @doc=Hpricot(Iconv.iconv("UTF-8//IGNORE","euc-kr//IGNORE",open("http://sampleshop.co.kr/template/1/categ_list.asp?categ_code=16&page=1&cv=6&orderOPT=0&pagesize=612").read).to_s)
   @addr=""
   @doc=(@doc/"table[@width='95%']")
   @doc.search("/tr[2]").remove
   @doc.search("tr:eq(1)").remove
   shuliang=Array.new
   jiage=Array.new
   imgsrc=Array.new
             shangpinshijian=Time.now-30
   mail=''
   mail1=''
   i=0
   @doc.search("td:eq(3) div").each do |item|#shuliang
     shuliang[i]=item
     i=i+1
   end
   i=0
   @doc.search("td:eq(2)").each do |item|#jiage
     jiage[i]=item
     i=i+1
   end
   i=0
   @doc.search("td:eq(0) a img").each do |item|#img
     imgsrc[i]='http://sampleshop.co.kr'+item['src'].to_s
     i=i+1
   end
   i=0
   @doc.search("td:eq(1) a").each do |item|#href
     imgurl='http://sampleshop.co.kr'+item['href'].to_s
     jiage1=jiage[i+1].to_s.scan(/\d+/)
     shuliang1=shuliang[i].to_s.scan(/\d+/).to_s
     unless Caijisp.find(:first, :conditions => "url = '#{imgurl}'")
         d=Caijisp.new()
         d.url=imgurl
         d.tupian=imgsrc[i]
         d.jiage=jiage1
         d.biaoti=item.innerText.to_s
         d.shuliang=shuliang1
         d.save
         mail=mail+"<a href=#{d.url}><img src=#{d.tupian}><br>#{d.biaoti} #{d.jiage} #{d.shuliang}</a><br>"
      else
        shangpin=Caijisp.find(:first, :conditions => "url = '#{imgurl}'")

        if shangpin.updated_at<shangpinshijian#检查时间找曾经下架的产品
         mail=mail+"<a href=#{shangpin.url}><img src=#{shangpin.tupian}><br>#{shangpin.biaoti} #{shangpin.jiage}上架 #{shangpin.laojiage}#{shangpin.shuliang}</a><br>"
        end

        shangpinshijian=shangpin.updated_at
        shangpin.laojiage=shangpin.jiage
         shangpin.jiage=jiage1
         shangpin.shangcigenxin=shangpin.updated_at
         shangpin.save
        if shangpin.jiage<shangpin.laojiage#降价产品
         mail=mail+"<a href=#{shangpin.url}><img src=#{shangpin.tupian}><br>#{shangpin.biaoti} #{shangpin.jiage}降价 #{shangpin.laojiage}#{shangpin.shuliang}</a><br>"
        end
      end
         i=i+1
     end
     @doc=''
     if mail.length>400
       Mymail.deliver_send("aontimer@gmail.com",mail)
     end
  end
  def mini
  require 'hpricot'
  require 'open-uri'
   shuliang=Array.new
   jiage=Array.new
   imgsrc=Array.new
   shangpinshijian=Time.now-30
   mail=''
   1.upto(8) do |page|
   @doc=""
   #http://www.mini1004.com/shopimages/mini1004/0670100000573.jpg
   @doc=Hpricot(Iconv.iconv("UTF-8//IGNORE","euc-kr//IGNORE",open("http://www.mini1004.com/shop/shopbrand.html?page=#{page}&xcode=067&scode=&type=X&search=&sort=brandname").read).to_s)
   @doc=(@doc/"table[@width='143']")
   i=0
   @doc.search("img[@align=center]").each do |item|#img
     imgsrc[i]='http://www.mini1004.com'+item['src'].to_s
     i=i+1
   end
   i=0
   @doc.search("td[@class=brandprice]").each do |item|#img
     jiage[i]=item.to_s.scan(/\d+/).to_s
     i=i+1
   end
   i=0
   @doc.search("td[@height=30] a").each do |item|#img
     imgurl='http://www.mini1004.com'+item['href'].to_s
     biaoti=item.at("font:eq(0)").innerText
     unless Caijisp.find(:first, :conditions => "url = '#{imgurl}'")
         d=Caijisp.new()
         d.url=imgurl
         d.tupian=imgsrc[i]
         d.jiage=jiage[i]
         d.biaoti=biaoti
         d.save
         mail=mail+"<a href=#{d.url}><img src=#{d.tupian}><br>#{d.biaoti} #{d.jiage} #{d.shuliang}</a><br>"
      else
        shangpin=Caijisp.find(:first, :conditions => "url = '#{imgurl}'")
       if shangpin.updated_at<shangpinshijian#检查时间找曾经下架的产品
         mail=mail+"<a href=#{shangpin.url}><img src=#{shangpin.tupian}><br>#{shangpin.biaoti} #{shangpin.jiage}上架 #{shangpin.laojiage}#{shangpin.shuliang}</a><br>"
        end
        shangpinshijian=shangpin.updated_at
        shangpin.laojiage=shangpin.jiage
         shangpin.jiage=jiage[i]
         shangpin.shangcigenxin=shangpin.updated_at
         shangpin.save
        if shangpin.jiage<shangpin.laojiage#降价产品
         mail=mail+"<a href=#{shangpin.url}><img src=#{shangpin.tupian}><br>#{shangpin.biaoti} #{shangpin.jiage}降价 #{shangpin.laojiage}#{shangpin.shuliang}</a><br>"
        end
     end
      i=i+1
   end
   end
     if mail.length>400
       Mymail.deliver_send("aontimer@gmail.com",mail)
     end
 end


  def index
    @caijisps = Caijisp.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @caijisps }
    end
  end

  # GET /caijisps/1
  # GET /caijisps/1.xml
  def show
    @caijisp = Caijisp.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @caijisp }
    end
  end

  # GET /caijisps/new
  # GET /caijisps/new.xml
  def new
    @caijisp = Caijisp.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @caijisp }
    end
  end

  # GET /caijisps/1/edit
  def edit
    @caijisp = Caijisp.find(params[:id])
  end

  # POST /caijisps
  # POST /caijisps.xml
  def create
    @caijisp = Caijisp.new(params[:caijisp])

    respond_to do |format|
      if @caijisp.save
        flash[:notice] = 'Caijisp was successfully created.'
        format.html { redirect_to(@caijisp) }
        format.xml  { render :xml => @caijisp, :status => :created, :location => @caijisp }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @caijisp.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /caijisps/1
  # PUT /caijisps/1.xml
  def update
    @caijisp = Caijisp.find(params[:id])

    respond_to do |format|
      if @caijisp.update_attributes(params[:caijisp])
        flash[:notice] = 'Caijisp was successfully updated.'
        format.html { redirect_to(@caijisp) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @caijisp.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /caijisps/1
  # DELETE /caijisps/1.xml
  def destroy
    @caijisp = Caijisp.find(params[:id])
    @caijisp.destroy

    respond_to do |format|
      format.html { redirect_to(caijisps_url) }
      format.xml  { head :ok }
    end
  end
end
